import { defineStore } from 'pinia'

export const useBlogStore = defineStore('blog', {
  state: () => ({
    blogs: [
      {
        id:1,
        title:'白日梦想家',
        author:'佚名1',
        content:'今天做了一个美梦'
      },
      {
        id:2,
        title:'白日梦想家',
        author:'佚名2',
        content:'今天做了一个美梦'
      },
      {
        id:3,
        title:'白日梦想家',
        author:'佚名3',
        content:'今天做了一个美梦'
      },
      {
        id:4,
        title:'白日梦想家',
        author:'佚名4',
        content:'今天做了一个美梦'
      },
      {
        id:5,
        title:'白日梦想家',
        author:'佚名5',
        content:'今天做了一个美梦'
      }
    ]
  }),
  actions: {
    // 编辑
    updateBlog(updatedBlog) {
      const index = this.blogs.findIndex(b => b.id === updatedBlog.id)
      if (index !== -1) this.blogs.splice(index, 1, updatedBlog)
    },
    // 添加
    addBlog(newBlog) {
      newBlog.id = this.blogs.length + 1
      this.blogs.push(newBlog)
    }
  }
})
